package com.example.myblog.controller;

import com.example.myblog.common.AjaxResult;
import com.example.myblog.common.ApplicationVariable;
import com.example.myblog.common.StringTools;
import com.example.myblog.common.UserSessionTools;
import com.example.myblog.entity.ArticleInfo;
import com.example.myblog.entity.UserInfo;
import com.example.myblog.entity.vo.ArticleInfoVO;
import com.example.myblog.service.ArticleService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request)
    {
        //非空效验
        if (articleInfo == null||
            !StringUtils.hasLength(articleInfo.getContent())||
            !StringUtils.hasLength(articleInfo.getTitle())) {
            return AjaxResult.fail(-1,"参数异常");
        }
        //得到用户uid
        HttpSession session=request.getSession();
        UserInfo userInfo=(UserInfo) session.getAttribute(ApplicationVariable.SESSION_USERINFO_KEY);
        articleInfo.setUid(userInfo.getId());
        int result=articleService.add(articleInfo);
        return AjaxResult.success(result);
    }
    @RequestMapping("/getArticleById")
    public AjaxResult getArticleById(Integer id,HttpServletRequest request){
        if (id == null||id<=0 ) {
            return AjaxResult.fail(-1,"参数有误");
        }
        System.out.println("id->"+id);
        UserInfo userInfo= UserSessionTools.getLoginUser(request);
        ArticleInfo articleInfo=articleService.getDetailByIdAndUid(id,userInfo.getId());
        System.out.println("uid->"+userInfo.getId());

        return AjaxResult.success(articleInfo);
    }
    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo,HttpServletRequest request)
    {
        if (articleInfo == null||articleInfo.getId()<=0||
            !StringUtils.hasLength(articleInfo.getContent())||
                    !StringUtils.hasLength(articleInfo.getTitle())) {
            return AjaxResult.fail(-1,"参数有误");
        }
        UserInfo userInfo=UserSessionTools.getLoginUser(request);
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        int result=articleService.update(articleInfo);
        return AjaxResult.success(result);
    }
    @RequestMapping("/getdetail")
    public AjaxResult getdetail(Integer id){
        if (id == null||id<=0) {
            return AjaxResult.fail(-1,"参数有误!");
        }
        ArticleInfoVO articleInfoVO=articleService.getDetail(id);
        return AjaxResult.success(articleInfoVO);
    }
    @RequestMapping("/addrcount")
    public AjaxResult addrcount(Integer id){
        if (id == null||id<=0) {
            return AjaxResult.fail(-1,"参数有误!");
        }
        int result=articleService.addRCount(id);
        return AjaxResult.success(result);
    }
    @RequestMapping("/mylist")
    public AjaxResult mylist(HttpServletRequest request){
        UserInfo userInfo=UserSessionTools.getLoginUser(request);
        List<ArticleInfo> list=articleService.getMyArticleList(userInfo.getId());
        for (ArticleInfo item :
                list) {
            String content= StringTools.subLength(item.getContent(),200);
            item.setContent(content);
        }
        return AjaxResult.success(list);
    }
    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request){
        if (id == null||id<=0) { return AjaxResult.fail(-1,"参数错误"); }
        UserInfo userInfo=UserSessionTools.getLoginUser(request);
        int result=articleService.del(id,userInfo.getId());
        return AjaxResult.success(result);
    }
    @RequestMapping("/getlistbypage")
    public AjaxResult getListByPage(Integer pageSize,Integer pageIndex){
        if (pageSize == null||pageSize<=0) { pageSize=2; }
        if (pageIndex == null||pageIndex<=0) { pageIndex=2; }
        int offset=(pageIndex-1)*pageSize;//分页公式
        List<ArticleInfo> list=articleService.getListByPage(pageSize,offset);
        //多线程并发执行循环
        list.stream().parallel().forEach(item->{
            item.setContent(StringTools.subLength(item.getContent(),150));
        });
        return AjaxResult.success(list);
    }
    @RequestMapping("/getcount")
    public AjaxResult getcount(){
        return AjaxResult.success(articleService.getCount()) ;
    }
}
